package com.controller.user;

import com.entity.User;
import com.service.user.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/** 响应个人信息页的请求 */
@RestController
@CrossOrigin
public class PersonalInfoController {
  @Autowired UserService userService;

  /**
   * 获取个人信息
   *
   * @param userId 用户编号
   * @return 用户信息
   */
  @PostMapping("personalInfo/getPersonalInfo")
  public User getPersonalInfo(@RequestParam("userId") String userId) {
    return userService.getPersonalInfo(userId);
  }

  /**
   * 修改用户昵称
   *
   * @param userId 用户编号
   * @param nickname 新的昵称
   * @return 修改成功返回1，失败返回0
   */
  @PostMapping("personalInfo/chNickname")
  public String chNickname(
      @RequestParam("userId") String userId, @RequestParam("nickname") String nickname) {
    int ret = userService.chNickname(userId, nickname);
    return String.valueOf(ret);
  }

  /**
   * 更换头像，保存头像并更新url
   *
   * @param userId 用户编号
   * @param file 头像文件
   * @return 若用户存在且文件是.jpg/png/gif/jpeg格式的图片，且更换成功，返回新的图片url,否则返回null
   */
  @PostMapping("personalInfo/chImage")
  public String chImage(
      @RequestParam("userId") String userId, @RequestParam("image") MultipartFile file) {
    return userService.chImage(userId, file);
  }

  /**
   * 修改基本信息
   *
   * @param userId 用户编号
   * @param realName 真实姓名
   * @param sex 性别
   * @param email 邮箱
   * @param birthday 生日
   * @param age 年龄
   * @param location 位置
   * @param elucidation 个人说明
   * @return 修改成功返回1，否则返回0
   */
  @PostMapping("personalInfo/chBasicInfo")
  public String chBasicInfo(
      @RequestParam("userId") String userId,
      @RequestParam("realName") String realName,
      @RequestParam("sex") char sex,
      @RequestParam("email") String email,
      @RequestParam("birthday") String birthday,
      @RequestParam("age") int age,
      @RequestParam("location") String location,
      @RequestParam("elucidation") String elucidation) {
    int ret =
        userService.chBasicInfo(userId, realName, sex, email, birthday, age, location, elucidation);
    return String.valueOf(ret);
  }

  /**
   * 修改教育信息
   *
   * @param userId 用户编号
   * @param school 学校
   * @param major 专业
   * @param enrollYear 入学年份
   * @param eduBackGround 学历
   * @return 修改成功返回1，否则返回0
   */
  @PostMapping("personalInfo/chEduInfo")
  public String chEduInfo(
      @RequestParam("userId") String userId,
      @RequestParam("school") String school,
      @RequestParam("major") String major,
      @RequestParam("enrollYear") int enrollYear,
      @RequestParam("eduBackGround") String eduBackGround) {
    int ret = userService.chEduInfo(userId, school, major, enrollYear, eduBackGround);
    return String.valueOf(ret);
  }

  /**
   * 修改标签
   *
   * @param userId 用户编号
   * @param label1Id 标签1编号
   * @param label2Id 标签2编号
   * @param label3Id 标签3编号
   * @return 修改成功返回1，否则返回0
   */
  @PostMapping("personalInfo/chLabel")
  public String chLabel(
      @RequestParam("userId") String userId,
      @RequestParam("label1Id") int label1Id,
      @RequestParam("label2Id") int label2Id,
      @RequestParam("label3Id") int label3Id) {
    int ret = userService.chLabel(userId, label1Id, label2Id, label3Id);
    return String.valueOf(ret);
  }
}
